<%--
  Created by IntelliJ IDEA.
  User: Enpi
  Date: 2021/9/3
  Time: 23:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
    String ctx = request.getContextPath();
    request.setAttribute("ctx", ctx);
%>
<link href="<%=request.getContextPath()%>/css/css.css" rel="stylesheet">
<html>
<head>
    <title>DDQ</title>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>Bootstrap</title>
    <%--    <link rel="stylesheet" type="text/css" href="css/css.css"/>--%>
    <link rel="stylesheet" href="<%=request.getContextPath()%>/bootstrap/bootstrap.css">
    <script type="text/javascript" src="<%=request.getContextPath()%>/bootstrap/jquery-1.9.1.min.js"></script>
    <script src="<%=request.getContextPath()%>/bootstrap/bootstrap.js"></script>
    <script src="<%=request.getContextPath()%>/bootstrap/jquery.validate.min.js"></script>
    <script src="<%=request.getContextPath()%>/bootstrap/validate.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath() %>/My97DatePicker/WdatePicker.js"></script>

</head>
<body>
<form action="/upd.do" method="post">
    <input type="hidden" name="sid" value="${param.sid}">
    <input type="hidden" name="shengi">
    <input type="hidden" name="shii">
    <input type="hidden" name="xiani">
    名称:<input type="text" name="sname"><br>
    省:
    <select name="sheng.id">
        <option value="">请选择</option>
    </select>
    市:
    <select name="shi.id">
        <option value="">请选择</option>
    </select>
    县:
    <select name="xian.id">
        <option value="">请选择</option>
    </select>
    <br>
    图片:
    <div class="photoUpLoad" >
        <input type="file"   class="photo" name="photo" accept="image/*"><span class="cross">+</span>
        <img src="" alt="" class="photoEcho" width="200px">
    </div>
    <br>
    状态:
    <input type="radio" name="status" value="0">上架
    <input type="radio" name="status" value="1">下架<br>
    介绍:<input type="text" name="title"><br>
    时间:<input type="date" name="starttime"><br>
    是否出入库:
    <select name="bid">
        <option value="">请选择</option>
    </select>
</form>

<input type="button" value="添加" onclick="add()">
</body>
<script>
    function add() {
        let  formData = new FormData($("form")[0])
        $.ajax({
            url:"upd.do",
            data:formData,
            dataType:"json",
            type:"post",
            contentType:false,
            processData:false,
            success:function (obj) {
                if(obj>0){
                    alert("添加成功")
                    location="list.do";
                }
            }
        })
    }
</script>


<script>
    $(function () {
        $.ajax({
            url:"getByid.do",
            data:{"sid":${param.sid}},
            dataType:"json",
            type:"post",
            success:function (obj) {
                $("[name='sname']").val(obj.sname);
                $("[name='shengi']").val(obj.sheng);
                $("[name='shii']").val(obj.shi);
                $("[name='xiani']").val(obj.xian);
                $("img").prop("src",obj.pic);
                $("[name='status']").prop("checked",true);
                $("[name='title']").val(obj.title);
                $("[name='starttime']").val(obj.starttime);
                $("[name='bid']").val(obj.bid);
            }
        })


        $.ajax({
            url:"listxl.do",
            dataType: "json",
            type: "post",
            success:function (obj) {
                for (let x in obj) {
                    $("[name='bid']").append(
                        "<option value="+obj[x].bid+" selected>"+obj[x].bname+"</option>\n"
                    )
                }
            }
        })


        $.ajax({
            url:"listgoods.do",
            data: {"pid":0},
            dataType: "json",
            type: "post",
            success:function (obj) {
                for (let x in obj) {
                    if(obj[x].id==$("[name='shengi']").val()){
                        $("[name='sheng.id']").append('<option value="'+obj[x].id+'" selected>'+obj[x].name+'</option>')
                        $("[name='sheng.id']").trigger("change")
                    }else {
                        $("[name='sheng.id']").append('<option value="'+obj[x].id+'">'+obj[x].name+'</option>')
                    }
                }
            }
        })

        $("[name='sheng.id']").change(function () {
            $("[name='shi.id']").empty();
            $("[name='shi.id']").append("        <option value=\"\">请选择</option>\n")
            $("[name='xian.id']").empty();
            $("[name='xian.id']").append("        <option value=\"\">请选择</option>\n")

            var pid = $(this).val()
            $.ajax({
                url:"listgoods.do",
                data: {"pid":pid},
                dataType: "json",
                type: "post",
                success:function (obj) {
                    for (let x in obj) {
                        if(obj[x].id==$("[name='shii']").val()){
                            $("[name='shi.id']").append('<option value="'+obj[x].id+'" selected>'+obj[x].name+'</option>')
                            $("[name='shi.id']").trigger("change")
                        }else {
                            $("[name='shi.id']").append('<option value="'+obj[x].id+'">'+obj[x].name+'</option>')
                        }
                    }
                }
            })
        })

        $("[name='shi.id']").change(function () {
            $("[name='xian.id']").empty();
            $("[name='xian.id']").append("        <option value=\"\">请选择</option>\n")

            var pid = $(this).val()
            $.ajax({
                url:"listgoods.do",
                data: {"pid":pid},
                dataType: "json",
                type: "post",
                success:function (obj) {
                    for (let x in obj) {
                        if(obj[x].id==$("[name='xiani']").val()){
                            $("[name='xian.id']").append('<option value="'+obj[x].id+'" selected>'+obj[x].name+'</option>')
                            $("[name='xian.id']").trigger("change")
                        }else {
                            $("[name='xian.id']").append('<option value="'+obj[x].id+'">'+obj[x].name+'</option>')
                        }
                    }
                }
            })
        })
    })
</script>

</html>
